package com.zhugang.week01;

/**
 * @program algorithms
 * @description: canJump
 * @author: chanzhugang
 * @create: 2022/03/30 00:09
 */
public class CanJump {

    public static void main(String[] args) {
        CanJump jump = new CanJump();
        int[] nums = new int[]{2, 3, 1, 1, 4};
        boolean flag = jump.canJump(nums);
        System.out.println(flag);

    }


    /**
     * 跳跃游戏 [2,3,1,1,4] true ;  [3,2,1,0,4] false
     *
     * @param nums
     * @return
     */
    public boolean canJump(int[] nums) {
        int reachedMax = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > reachedMax) {
                return false;
            }
            if (i + nums[i] > reachedMax) {
                reachedMax = i + nums[i];
            }
            if (reachedMax >= nums.length - 1) {
                return true;
            }
        }
        return false;
    }
}